<template>
  <div class="login-wrapper">
    <div class="modal">
      <el-form ref="userForm" :model="user" status-icon :rules="rules">
        <div class="title">火星</div>
        <el-form-item prop="userName">
          <el-input type="text" prefix-icon="el-icon-user" v-model="user.userName" />
        </el-form-item>
        <el-form-item prop="userPwd">
          <el-input type="password" prefix-icon="el-icon-view" v-model="user.userPwd" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn-login" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import storage from "./../utils/storage";
import utils from "./../utils/utils";
// import { defineAsyncComponent } from 'vue';
// const _import=(path) => defineAsyncComponent(() => import(/* @vite-ignore */ `../views${path}.vue`));
// const _import=(path) => () => import(/* @vite-ignore */ `../views${path}.vue`);
// () => import('./MyPage.vue')
// const modules=import.meta.glob("../views*.vue");
const modules = import.meta.glob("../views/**/*.vue");
export default {
  name: "login",
  data() {
    return {
      user: {
        userName: "mjs",
        userPwd: "123456"
      },
      rules: {
        userName: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          }
        ],
        userPwd: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    login() {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          this.$api.login(this.user).then(async (res) => {
            this.$store.commit("saveUserInfo", res);
            await this.loadAsyncRoutes();
            this.$router.push("/welcome");
          });
        } else {
          return false;
        }
      });
    },
    async loadAsyncRoutes() {
      const userInfo = storage.getItem("userInfo") || {};
      if (userInfo.token) {
        try {
          const { menuList } = await this.$api.getPermissionList();
          const routes = utils.generateRoute(menuList);
          routes.map((route) => {
            // let url=`./../views/${route.component}.vue`;
            // route.component=() => import(/* @vite-ignore */`${url}`);
            // let url=route.component;
            // route.component=_import(/* @vite-ignore */ url);
            const url = `../views${route.component}.vue`; // componet => /Menu
            // console.log(modules,url,modules[url])
            route.component = modules[url];
            if (route.component) this.$router.addRoute("home", route);
            // addRouter 弃用 只有addRoute
            // console.log(this.$router)
          });
        } catch (error) {
          console.log("error:", error);
        }
      }
    },
    goHome() {
      this.$router.push("/welcome");
    }
  }
};
</script>

<style lang="scss">
.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f9fcff;
  width: 100vw;
  height: 100vh;

  .modal {
    width: 500px;
    padding: 50px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0px 0px 10px 3px #c7c9cb4d;

    .title {
      font-size: 50px;
      line-height: 1.5;
      text-align: center;
      margin-bottom: 30px;
    }

    .btn-login {
      width: 100%;
    }
  }
}
</style>